package com.zx.map.dao.impl;

import com.zx.map.dao.AgentInfoDao;
import com.zx.map.dao.mapper.AgentInfoMapper;
import com.zx.map.dao.model.AgentInfo;
import com.zx.map.dao.model.AgentInfoExample;
import com.zx.platform.dao.mapper.SequenceMapper;

import com.zx.platform.utils.StrUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-1
 * Time: 上午10:54
 * To change this template use File | Settings | File Templates.
 */
@Component("AgentInfoDao")
public class AgentInfoImpl implements AgentInfoDao {

    @Autowired
    private AgentInfoMapper mapper;

    @Autowired
    private SequenceMapper sequencemapper;

    /**
     * 新增
     */
    public String add(AgentInfo agentInfo) throws DataAccessException {
        /**
         *  add by wenjw 2015.5.13
         *  操作员O+sequence(operator)
         商户 M+sequence（merchant）
         代理商 A+sequence（agent）
         */
        agentInfo.setAgentId("A" + sequencemapper.selectByStrAndNextval("agent"));
        //设置bsid edit by wenjw 2016.9.4
        if(StringUtils.isNotEmpty(agentInfo.getBsid())){
        	agentInfo.setBsid(agentInfo.getBsid()+"-"+agentInfo.getAgentId());
        }else{
        	agentInfo.setBsid(agentInfo.getAgentId());
        }
        agentInfo.setCreateDate(new Date());
        int column = mapper.insert(agentInfo);
        if (column != 1) {
            throw new RuntimeException("");
        }
        return agentInfo.getAgentId();
    }

    /**
     * 修改
     *
     * @param agentInfo
     * @return
     * @throws DataAccessException
     */
    public String update(AgentInfo agentInfo) throws DataAccessException {
        int column = mapper.updateByPrimaryKeySelective(agentInfo);
        if (column != 1) {
            throw new RuntimeException("");
        }
        return agentInfo.getAgentId();
    }

    /**
     * 删除
     */
    public void delete(AgentInfo agentInfo) throws DataAccessException {
        //PkA+sequence

        int column = mapper.deleteByPrimaryKey(agentInfo.getAgentId());
        if (column != 1) {
            throw new RuntimeException("");
        }
    }

    /**
     * 分页查找
     */
    public List<AgentInfo> getPageByExample(int pageSize, int pageBegin,
                                            AgentInfoExample example) throws DataAccessException {
        return mapper.getPageByModel(pageSize, pageBegin, example);
    }

    /**
     * 查找条数
     *
     * @param example
     * @return
     * @throws DataAccessException
     */
    public int countByExample(AgentInfoExample example) throws DataAccessException {
        return mapper.countByExample(example);
    }

    /**
     *
     */
    public List<AgentInfo> getListByExample(AgentInfoExample example)
            throws DataAccessException {
        return mapper.selectByExample(example);
    }

    //查找当前代理商下级id集合
    @Override
    public List<String> getDownAgentIdList(AgentInfoExample example) throws DataAccessException {
        return mapper.getDownAgentIdList(example);
    }

    /**
     * 根据代理商编号获取代理商信息
     */
    public AgentInfo selectByPrimaryKey(String key) throws DataAccessException {
        return mapper.selectByPrimaryKey(key);
    }

    public int updateByExampleSelective(AgentInfo agentInfo,AgentInfoExample agentInfoExample){
        return mapper.updateByExampleSelective(agentInfo, agentInfoExample);
    }

    /*更改从属关系
     */
    @Override
    public int updateAgentBelong(String fromBsidStart, String toBsidStart) {
        if(StrUtils.isNotBlank(fromBsidStart)&&StringUtils.isNotBlank(toBsidStart)){
            return mapper.updateAgentBelong(fromBsidStart,toBsidStart);
        } else {
           throw new RuntimeException("bsid不能空");
        }

    }
}
